<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="shortcut icon" href="../../favicon.ico" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="../../css/reset.css" />
    <link rel="stylesheet" type="text/css" href="../../css/login.css" />
</head>

<body>
    <div class="pos">
        <div class="login">
            <span class="close iconfont icon-close"></span>
            <img src="../../img/login/loginlogo.png" alt="">
            <ul>
                <li></li>
                <li class="phone">使用手机号登陆</li>
                <li></li>
            </ul>
            <form action="" id="form">
                <input style="margin-top: 0;" type="text" name="tel" class="tel" id="" placeholder="请输入您的手机号">
                <input type="password" name="pass" class="pass" id="" placeholder="请输入密码">
                <!-- <span class="tips">密码或账号不对</span> -->
                
                <input type="text" name="codes" class="codes" id="" placeholder="请输入验证码"  > 
                <div id="imgcodes"></div>
                
               
                <button class="loginbtn" type="button">
                    登陆
                </button>
            </form>
            <p>
                <span>
                    还没有账号?
                    <a href="./register.html">点击注册</a>
                </span>
            </p>
        </div>
    </div>
    <!-- <script src="../../utils/utils.js"></script> -->
    <script src="../../js/jquery.js"></script>
    <script src="../../js/common.js"></script>
    <script>
        var syscode = '';
        //创建验证码
        async function geneCode() {
            let [err, datas] = await sendAjax('/codeimg');
            console.log(err, datas);
            if (err) {
                $("#imgcodes").html(datas.data[0]);
                syscode = datas.data[1]; //系统生成的验证码
            }
        }

        geneCode();


        //刷新验证码
        $("#imgcodes").click(function() {
            geneCode();
        });


        $(".loginbtn").click(async function() {
            //获取用户输入的帐号
            let user = $(".tel").val();
            //获取用户输入的密码
            let pass = $(".pass").val();
            //获取用户输入的验证码
            let codes = $(".codes").val();

            if (user == '' || pass == '' || codes == '') {
                alert('帐号、密码及验证码都不能为空');
                return false;
            }


            let [e, d] = await sendAjax('/login', 'post', {
                user,
                pass,
                codes, //用户输入的验证码
                syscode //系统生成的验证码
            });
            console.log(e, d);
            if (e) {
                alert(d.msg);
                
                if (d.status == 200) { //登录成功
                    localStorage['jwtdatas'] = d.data[0];
                    location.href='../../index.html';
                }
            } else {
                alert('登录失败，稍后重试');
            }
        });
    </script>
</body>

</html>